
package com.qijia.common.listener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.RememberMeAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.web.authentication.WebAuthenticationDetails;


public class AuthenticationListener implements ApplicationListener {
	private final static transient Log	logger	= LogFactory
														.getLog(AuthenticationListener.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
	 */
	public void onApplicationEvent(ApplicationEvent event) {
		if (event instanceof AbstractAuthenticationEvent) {
			try {
				AbstractAuthenticationEvent authEvent = (AbstractAuthenticationEvent) event;
				
				if (event instanceof AuthenticationSuccessEvent) {
					if(authEvent.getSource() instanceof UsernamePasswordAuthenticationToken){
						//登录成功日志
						UsernamePasswordAuthenticationToken source = (UsernamePasswordAuthenticationToken)authEvent.getSource();
						WebAuthenticationDetails detail = (WebAuthenticationDetails)source.getDetails();
						//String procUsername = ((AppUser) authEvent.getAuthentication().getPrincipal()).getUsername();
						//String requestUrl = detail.getRemoteAddress();
						//AppAuditHelper.getInstance().doAuditAction(AppAuditHelper.ACTION_LOGIN, event.toString(), AppAuditHelper.RESULT_SUCCESS,requestUrl , procUsername);
					}else if(authEvent.getSource() instanceof RememberMeAuthenticationToken){
						//登录(记住我)成功日志
						RememberMeAuthenticationToken source = (RememberMeAuthenticationToken)authEvent.getSource();
						WebAuthenticationDetails detail = (WebAuthenticationDetails)source.getDetails();
						//String procUsername = ((AppUser) authEvent.getAuthentication().getPrincipal()).getUsername();
						//String requestUrl = detail.getRemoteAddress();
						//AppAuditHelper.getInstance().doAuditAction(AppAuditHelper.ACTION_REMEBER_ME_LOGIN, event.toString(), AppAuditHelper.RESULT_SUCCESS,requestUrl , procUsername);
					}
				}else if (event instanceof AbstractAuthenticationFailureEvent) {
				    if(authEvent.getSource() instanceof UsernamePasswordAuthenticationToken){
						//登录失败日志
						UsernamePasswordAuthenticationToken source = (UsernamePasswordAuthenticationToken)authEvent.getSource();
						WebAuthenticationDetails detail = (WebAuthenticationDetails)source.getDetails();
						//String procUsername = (String) authEvent.getAuthentication().getPrincipal();
						//String requestUrl = detail.getRemoteAddress();
						//AppAuditHelper.getInstance().doAuditAction(AppAuditHelper.ACTION_LOGIN, event.toString(), AppAuditHelper.RESULT_FAILED,requestUrl , procUsername);
					}
				}

			} catch (Throwable e) {
				logger.error("Cannot audit Login event:" + event.toString(), e);
			}
		}

	}

}
